package com.msn.sign;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

import com.msn.sign.util.ConfigCenter;
import com.msn.sign.util.Constant;
import com.msn.sign.util.DateUtil;
import com.msn.sign.util.SignSearch;

public class MainActivity extends Activity {
	
	private static final int MENU_SEND_SMS = 1;
	
	private static final int LOAD_DIALOG = 2;
	
	private static final int CONTENT_LOAD_FINISH = 10;
	
	public static final String DATE_FORMAT = "yyyy/MM/dd";
	
	private ListView signList;
	
	private Context context;	

	private TextView pre;
	
	private TextView next;
	
	private Calendar calendar = Calendar.getInstance();
	
	private TextView curDay;
	
	private List<String> contentList = new ArrayList<String>();
	
	private String curItem;
	
	private Handler handler = new Handler() {
		public void handleMessage(Message message) {
			switch (message.what) {
			case CONTENT_LOAD_FINISH:				
				dismissDialog(LOAD_DIALOG);
				String[] array = new String[contentList.size()];
		    	contentList.toArray(array);
				signList.setAdapter(new ArrayAdapter<String>(MainActivity.this,
		                R.layout.list_item, array));    
				break;
			}
			super.handleMessage(message);
		}
	};
	
	class LoadThread extends Thread{
		
		private String date;
		
		public LoadThread(String date){
			this.date = date;
		}
		
		public void run(){
			
			showList(date);
			Message loadFinish = new Message();
			loadFinish.what = CONTENT_LOAD_FINISH;
			handler.sendMessage(loadFinish);
		}
	}
	
	
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main);
        context = getApplicationContext();
        
        signList = (ListView)this.findViewById(R.id.signList);
        curDay = (TextView)this.findViewById(R.id.cur_day);
        pre = (TextView)this.findViewById(R.id.btn_pre);
        next = (TextView)this.findViewById(R.id.btn_next);
        
       
        
        String today = DateUtil.getCurrentDate(DATE_FORMAT);
        curDay.setText("当日最佳签名："+today);    
        showDialog(LOAD_DIALOG);
        new LoadThread(today).start();
        
        
        pre.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				showDialog(LOAD_DIALOG);
				
				long calendarLong = Long.valueOf(DateUtil.formatDate(calendar.getTime(), "yyyyMMdd"));
				long todayLong = Long.valueOf(DateUtil.getCurrentDate("yyyyMMdd"));
					
				if(calendarLong<todayLong){
					calendar.add(Calendar.DAY_OF_MONTH, 1);
				}else{
					Toast.makeText(context, "已经是第一页了！", Toast.LENGTH_SHORT).show();
				}
				
				String date = DateUtil.formatDate(calendar.getTime(), DATE_FORMAT);
				curDay.setText("当日最佳签名："+date);  
				new LoadThread(date).start();
			}        	
        });
        next.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				showDialog(LOAD_DIALOG);
				calendar.add(Calendar.DAY_OF_MONTH, -1);
				String date = DateUtil.formatDate(calendar.getTime(), DATE_FORMAT);
				curDay.setText("当日最佳签名："+date);    
				new LoadThread(date).start();
				
			}        	
        });
        
        signList.setOnItemClickListener(new OnItemClickListener(){
			@Override
			public void onItemClick(AdapterView<?> adapterView, View arg1, int position,
					long id) {
				
				curItem = contentList.get(position);
				registerForContextMenu(adapterView);
			}
        	
        });
        
        if(!ConfigCenter.getValue(context, Constant.KEY_WIDGET_ADDED, false)){
        	Toast.makeText(context, "温馨提示：您还可以在桌面添加小部件快捷浏览", Toast.LENGTH_LONG).show();
        }
        
    }
    
    private void showList(String date){    	
    	
    		
    	
		try {
			contentList = SignSearch.search(date);
		} catch (IOException e) {
			Toast.makeText(context, "获取数据失败！", Toast.LENGTH_SHORT).show();
		}
    }
    
    private void prepareSms(String content){
    	Intent it = new Intent(Intent.ACTION_VIEW);    
    	it.putExtra("sms_body", content);    
    	it.setType("vnd.android-dir/mms-sms");    
    	startActivity(it);  
    }
    
    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {           
            case LOAD_DIALOG: {
                ProgressDialog dialog = new ProgressDialog(this);
                dialog.setMessage("加载中...");
                dialog.setIndeterminate(true);
                dialog.setCancelable(true);
                return dialog;
            }
        }
        return null;
    }
    
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo){
    	super.onCreateContextMenu(menu, v, menuInfo);	 
    	menu.add(0, MENU_SEND_SMS, 0, R.string.send_sms);
    }
    
    public boolean onContextItemSelected (MenuItem item) {
    	super.onContextItemSelected(item);    	
    	switch(item.getItemId()){
    	case MENU_SEND_SMS:
    		if(curItem!=null && !"".equals(curItem)){
    			prepareSms(curItem);
    		}
    		break;    	
    	}
    	
    	return false;
    }
}